﻿<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>某产品的日平均销售额计算</title>
	</head>
	<body>
		<script type="text/javascript">
			var WeekSales = function () {
				this.month = [];
				this.init = init;
				this.getAverageSomeMonth = 	getAverageSomeMonth;
				this.getAverageSomeWeek = getAverageSomeWeek;
			};

			var init = function (month) {
				var i = 0,j = 0,k = 0;
				for (i = 0; i < month; i++) {
					this.month[ i ] = [];
					for (k = 0; k < 4; k++) {
						this.month[ i ][ k ] = [];
						for(j = 0; j < 7; j++) {
							this.month[ i ][ k ][ j ] = parseInt(Math.random()*10000);
						}
						console.log((i+1)+"月"+(k+1)+"周："+(this.month[ i ][ k ]).toString());					
					}
				}
			};

			function getAverageSomeMonth(month) {
				month = month || 12;
				console.log(month + "月的日平均销售额是:" + this.month[ month - 1 ].map(function(arr) {
					return arr.reduce(function(a, b){return a + b;});
				}).reduce(function(a, b){return a + b;}) / 28);
			}

			function getAverageSomeWeek(month,week) {
				month = month || 12;
				week = week || 1;
				console.log(month + "月第" + week + "周的日平均销售额是:" + this.month[ month - 1 ][ week - 1 ].reduce(function(a, b){return a + b;}) / 7);
			}

			var newWeekSales = new WeekSales();
			newWeekSales.init(12);
			newWeekSales.getAverageSomeMonth(12);
			newWeekSales.getAverageSomeWeek(12,4);
		</script>
	</body>
</html>
